<?php
/**
 * 
 * Controlado de estatus control interno
 * @author natanael
 * 
 */
class Ciestatus extends Controller{

       public function __construct()
       {
          parent::Controller();
          $this->load->model(array('mciestatus'));
       }
       
       public function index()
       {
          $this->load->view('main', array('content_page' => 'ciestatus_list','menutop' => 0));
       }
       
       public function add()
	   {
	       $this->load->view('main', array('content_page' => 'ciestatus_add','menutop' => 0));
	   }
	   
       public function edit($id)
	   {
	   	   $objetos = $this->mciestatus->LoadStatusById($id);
	       $this->load->view('main', array('content_page' => 'ciestatus_edit','menutop' => 0, 'objetos'=>$objetos));
	   }
	   
       public function delete()
	   {
	      $ids = $this->input->post('delete_rows');
	   
	      if ($this->mciestatus->delete($ids) === true) {
	   	   
	      }else{
	         
	      } 
	   
	   }
       
       public function data()
	   {
	   	
    	     $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		     $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'ID'; // Columna para el order by
		     $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		     $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;
		     $start = $limit * $page - $limit;
		     if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );

		     $records = $this->mciestatus->loadEstatus($start, $limit, $sortname, $sortorder);

		     $totalRecords = count($records);

		     $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) $page = $totalPages;

		        $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		       if($records != false) 
		       {
			     foreach($records as $objetos){
			     	
				     $arr = $json_output['rows'][] = array(
				                'ID'          => $objetos->getIdStatus(),
				                'Detalles'    => $objetos->getDescripcion()
				      );
			      }
		        }

		 echo json_encode($json_output);
		
	   }
	   
      public function save()
	  {	
	               $data = array('Descripcion'  => strtoupper($this->input->post('description')));
		
		          if ($this->input->post('id') == false){
				 
			            if ($this->mciestatus->crear($data) == true){
			                 $this->session->set_flashdata('error_title', 'Se a creado el registro');
			            }else{
			 	             $this->session->set_flashdata('error_title', 'Huvo un error');
			            } 
			
		           }else{
			              if ($this->mciestatus->edit($data,$this->input->post('id')) == true){
			                  $this->session->set_flashdata('error_title', 'Se Edit el registro');
			              }else{
			 	              $this->session->set_flashdata('error_title', 'Huvo un error');
			              }        
			       }
		redirect('ciestatus');	
	 }


}